package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoInformation;
import com.muyu.danmo.domain.DanmoProvince;
import com.muyu.danmo.domain.DanmoUserVisitors;
import com.muyu.danmo.domain.vo.DanmoPersonInfoVo;
import com.muyu.danmo.req.DanmoAncientDynastiesReq;
import com.muyu.danmo.req.DanmoInformationContyReq;
import com.muyu.danmo.service.IDanmoInformationService;
import com.muyu.danmo.service.IDanmoUserVisitorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 个人信息Controller
 *
 * @author yangle
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/information")
public class DanmoInformationController extends BaseController {
    @Autowired
    private IDanmoInformationService danmoInformationService;

    /**
     * 查询个人信息列表
     */
//    @RequiresPermissions("system:information:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoInformation danmoInformation) {

        //请求传输时间：请求发出时间戳到请求到达服务器时间戳之间的差值。
        //服务器处理时间：请求到达服务器时间戳到响应生成时间戳之间的差值。
        //响应传输时间：响应生成时间戳到响应到达客户端时间戳之间的差值。
        //总响应时间：请求发出时间戳到响应到达客户端时间戳之间的差值。
        try {
            //开始时间
            long startTime = System.currentTimeMillis();
            List<DanmoInformation> danmoInformations = danmoInformationService.selectDanmoInformationList(danmoInformation);
         //结束时间
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            System.out.println("查询单个信息耗时：" + time + "毫秒");
            return Result.success(danmoInformations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 导出个人信息列表
     */
//    @RequiresPermissions("system:information:export")
//    @Log(title = "个人信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DanmoInformation danmoInformation)
//    {
//        Map<String, List<DanmoInformation>> list = danmoInformationService.selectDanmoInformationList(danmoInformation);
//        ExcelUtil<DanmoInformation> util = new ExcelUtil<DanmoInformation>(DanmoInformation.class);
//        util.exportExcel(response, list, "个人信息数据");
//    }

    /**
     * 获取个人信息详细信息
     */
//    @RequiresPermissions("system:information:query")
    @PostMapping("/informationQuery")
    public Result getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }

        Long userId = loginUser.getSysUser().getUserId();
        DanmoInformation danmoInformation = danmoInformationService.selectDanmoInformationUserId(userId);
        if (danmoInformation == null) {
            return Result.error("请先完善个人信息");
        }
        return Result.success(danmoInformation);
    }


    /**
     * 新增个人信息
     */
//    @RequiresPermissions("system:information:add")
    @Log(title = "个人信息", businessType = BusinessType.INSERT)
    @PostMapping("/informationAdd")
    public Result add(@RequestBody DanmoInformation danmoInformation) {

//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser==null){
//            return Result.notLoggedIn("请登录");
//        }
//        Long userId = loginUser.getUserid();
//        danmoInformation.setUserId(userId);
        return danmoInformationService.insertDanmoInformation(danmoInformation);
    }

    /**
     * 修改个人信息
     */
//    @RequiresPermissions("system:information:edit")
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoInformation danmoInformation) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        danmoInformation.setUserId(userId);
//        DanmoInformation danmoInformation1 = new DanmoInformation();
//        danmoInformation1.setName(danmoInformation.getName());
//        List<DanmoInformation> danmoInformations = danmoInformationService.selectDanmoInformationList(danmoInformation1);
//        if (danmoInformations.size()>0){
//            return Result.error("该用户已存在");
//        }

        return danmoInformationService.updateDanmoInformation(danmoInformation);
    }

    /**
     * 删除个人信息
     */
//    @RequiresPermissions("system:information:remove")
    @Log(title = "个人信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return danmoInformationService.deleteDanmoInformationByIds(ids);

    }

    /**
     * 获取所有的省
     */
    @PostMapping("SelectProvince")
    public Result selectProvince(@RequestParam(required = false) String cityCode, @RequestParam(required = false) String countyCode) {
        List<DanmoProvince> danmoProvinces = danmoInformationService.selectProvince(cityCode, countyCode);
        return Result.success(danmoProvinces);
    }

    /**
     * 通过县编码查询人
     */
    @PostMapping("/selectCounty")
    public Result selectCounty(@RequestBody DanmoInformationContyReq danmoInformationContyReq) {
        List<DanmoInformation> danmoInformations = danmoInformationService.selectCounty(danmoInformationContyReq);
        return Result.success(danmoInformations);
    }

    /**
     * 通过古代朝代查询人
     */
    @PostMapping("/selectDynasty")
    public Result selectDynasty(@RequestBody DanmoAncientDynastiesReq danmoAncientDynastiesReq) {
        List<DanmoInformation> danmoInformations = danmoInformationService.selectDynasty(danmoAncientDynastiesReq);
        return Result.success(danmoInformations);
    }

    /**
     * 名家推荐
     */
    @PostMapping("/recommended")
    public Result recommended(@RequestBody DanmoInformation danmoInformation) {
        PageHelper.startPage(danmoInformation.getPageNum(),danmoInformation.getPageSize());
        List<DanmoPersonInfoVo> list = danmoInformationService.selectrecommended(danmoInformation);
        PageInfo<DanmoPersonInfoVo> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }
    /**
     * 名家推荐 书画和绘画
     */
    @PostMapping("/calligraphy")
    public Result calligraphy(@RequestBody DanmoInformation danmoInformation) {
        PageHelper.startPage(danmoInformation.getPageNum(),danmoInformation.getPageSize());
        List<DanmoPersonInfoVo> list = danmoInformationService.calligraphy(danmoInformation);
        PageInfo<DanmoPersonInfoVo> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    @Autowired
    private IDanmoUserVisitorsService danmoUserVisitorsService;

    /**
     * 查询个人信息
     *
     * @param informationId
     * @return
     */
    @PostMapping("/selectInformationPortfolio/{informationId}")
    public Result selectInformationPortfolio(@PathVariable Long informationId) {

//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser==null){
//            return Result.notLoggedIn("请登录");
//        }
        DanmoInformation danmoInformation = danmoInformationService.selectDanmoInformationById(informationId);
        DanmoUserVisitors danmoUserVisitors = new DanmoUserVisitors();
//        danmoUserVisitors.setUserId(danmoInformation.getUserId());
//        danmoUserVisitors.setVisitorName(danmoInformation.getName());
        danmoUserVisitors.setVisitTime(DateUtils.getNowDate());
//        danmoUserVisitorsService.insertDanmoUserVisitors(danmoUserVisitors);

        if (danmoInformation == null) {
            return Result.error("未找到相关信息");
        }
        return Result.success(danmoInformation);
    }

//    /**
//     * 查询他人
//     * @return
//     */
//    @PostMapping("/selectTaUser/{userId}")
//    public Result selectTaUser (@PathVariable Long userId) {
//        DanmoInformation danmoInformation = danmoInformationService.selectDanmoInformationUserId(userId);
//        return Result.success(danmoInformation);
//    }

    /**
     * 列表查询（省的拼音首字母包裹）
     *
     * @param danmoInformation
     * @return
     */
    @PostMapping("/litttt")
    public Result listttt(@RequestBody DanmoInformation danmoInformation) {
        try {
//            开始时间
            long startTime = System.currentTimeMillis();
            Map<String, Object> danmoInformations = danmoInformationService.list(danmoInformation);
//            结束时间
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            System.out.println("查询单个信息耗时：" + time + "毫秒");
            return Result.success(danmoInformations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
//        return danmoInformationService.list(danmoInformation);
    }

    /**
     * 查询古代
     *
     * @param danmoInformation
     * @return
     */
    @PostMapping("/selectAncient")
    public Result selectAncient(@RequestBody DanmoInformation danmoInformation) {
        List<DanmoInformation> danmoAncientDynasties = danmoInformationService.selectAncient(danmoInformation);
        return Result.success(danmoAncientDynasties);
    }

    /**
     * 查询社会团体  ；。、
     */
    @PostMapping("/selectMass")
    public Result selectMass(@RequestBody DanmoInformation danmoInformation) {
        List<DanmoInformation> danmoMass = danmoInformationService.selectMass(danmoInformation);
        return Result.success(danmoMass);
    }

    /**
     * 查询书画爱好者
     */
    @PostMapping("/selectArtLover")
    public Result selectArtLover(@RequestBody DanmoInformation danmoInformation) {
        List<DanmoInformation> danmoArtLover = danmoInformationService.selectArtLover(danmoInformation);
        return Result.success(danmoArtLover);
    }


    @PostMapping("/selectgetInfo")
    public Result getInfo(@RequestBody DanmoInformation danmoInformation) {
        return Result.success(danmoInformationService.getInfo(danmoInformation));
    }

    /**
     * 列表查询 省排序
     * @param danmoInformation
     * @return
     */
    @PostMapping("/select")
    public Result select(@RequestBody DanmoInformation danmoInformation) {
        List<Map<String, List<Map<String, List<DanmoInformation>>>>> select = danmoInformationService.select(danmoInformation);
        return Result.success(select);
    }

    /**
     * 列表查询名字排序
     * @param danmoInformation
     * @return
     */
    @PostMapping("/selectList")
    public Result  selectList(@RequestBody DanmoInformation danmoInformation){
        List<Map<String, List<DanmoInformation>>> list = danmoInformationService.selectList(danmoInformation);
        return Result.success(list);

    }

    /**
     * F
     * @param danmoInformation
     * @return
     */
    @PostMapping("/selectCouncil")
    public Result selectCouncil(@RequestBody DanmoInformation danmoInformation) {
        List<Map<String, Map<String, List<Map<String, DanmoInformation>>>>> councils = danmoInformationService.selectCouncil(danmoInformation);
        return Result.success(councils);
    }

    /**
     * 查询省市
     */
    @PostMapping("/selectCities")
    public Result selectCities(@RequestBody DanmoInformation danmoInformation) {
        return Result.success(danmoInformationService.selectCities(danmoInformation));
    }

    /**
     * 年份
     */
    @PostMapping("/selectListyears")
    public Result selectListyears(@RequestBody DanmoInformation danmoInformation){
        return Result.success(danmoInformationService.selectListyears(danmoInformation));
    }

    /**
     * 历届
     */
    @PostMapping("/selectSuccessive")
    public  Result selectSuccessive(@RequestBody DanmoInformation danmoInformation){
        return Result.success(danmoInformationService.selectSuccessive(danmoInformation));
    }
}
